package com.pro.controller;

import com.pro.domain.Member;
import com.pro.domain.Seat;
import com.pro.service.MemberService;
import com.pro.service.MemberServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
public class RegistAndLoginController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /*注册接口*/ /*http://192.168.104.64:9000/toRegist*/
    @CrossOrigin
    @RequestMapping("toRegist")
    public Member toRegist(String username, String password, String nickname){

        Member member = new Member();
        member.setUsername(username);
        member.setNickname(nickname);
        member.setPassword(password);
        memberService.createMember(username,password,nickname);
        return member;
    }

    /*登录接口*/ /*http://192.168.104.64:9000/toLogin*/
    @CrossOrigin
    @RequestMapping("toLogin")
    public Map toLogin(String username, String password){
        Map map = memberService.checkLogin(username, password);
        /*传进redis的方法*/
        Map<String,String> map1 = new HashMap<String,String>();
        System.out.println(map);
        Member member = (Member)map.get("token");
        if((Member)map.get("token")==null){
            return map;
        }else if((Member)map.get("token")!=null){
            String memberid1 =Long.toString(member.getMemberid());
//            String username1 = member.getUsername();
//            String password1 = member.getPassword();
//            String nickname1 = member.getNickname();
//            redisTemplate.boundHashOps("loginMember").put("memberid",memberid1);
//            redisTemplate.boundHashOps("loginMember").put("username",username1);
//            redisTemplate.boundHashOps("loginMember").put("password",password1);
//            redisTemplate.boundHashOps("loginMember").put("nickname",nickname1);
            redisTemplate.boundValueOps("memberId").set(memberid1);
        }
        return map;
    }

    /*从redis中取出memberid*/
    @CrossOrigin
    @RequestMapping("/redisMemberid")
    public String redisMemberid(){
        String memberid = redisTemplate.boundValueOps("memberId").get();
        if(memberid==null){
            return "0";
        }
        return memberid;
    }

}
